package cn.ffcs.uec.outStaff.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffManageEditBean;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffCostH;
import cn.ffcs.uec.outStaff.model.UecOutStaffManage;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import cn.ffcs.uom.systemconfig.manager.IdentityCardConfigManager;

@Controller
@Scope("prototype")
public class UecOutStaffManageEditComposer extends BasePortletComposer {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3695440774892813761L;

	private UecOutStaffManageEditBean bean = new UecOutStaffManageEditBean();

	private String opType;
	
	private UecOutStaffCostH uecOutStaffCostH;

	@Resource
	private UecOutStaffManager uecOutStaffManager;

	@Resource
	private IdentityCardConfigManager identityCardConfigManager;

	
	private UecOutStaffManage uecOutStaffManage;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
		
		
	}

	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$uecOutStaffManageEditWin() throws Exception {
		bindCombobox();
		opType = (String) arg.get("opType");
		if (opType.equals("add")) {
			bean.getUecOutStaffManageEditWin().setTitle("新增");
			
		} else if (opType.equals("edit")) {
			strToDate();
			bean.getUecOutStaffManageEditWin().setTitle("修改");
			PubUtil.fillBeanFromPo(arg.get("uecOutStaffManage"), bean);
		} else if (opType.equals("view")) {
			strToDate();
			bean.getUecOutStaffManageEditWin().setTitle("查看");
			PubUtil.disableComponent(bean.getUecOutStaffManageEditWin());
			PubUtil.fillBeanFromPo(arg.get("uecOutStaffManage"), bean);
		}
	}
	/**
	 * 绑定下拉框 绑定combobox
	 */
	private void bindCombobox() {
		List<NodeVo> nodeVoIsSendList = new ArrayList<NodeVo>();
		String[][] isSend = {{"Y", "是"}, {"N", "否"}};
		for (int i = 0; i < isSend.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(isSend[i][j]);
				nodeVo.setName(isSend[i][j + 1]);
			}
			nodeVoIsSendList.add(nodeVo);
		}
		
		ListboxUtils.rendererForEdit(bean.getNewMark(),
				nodeVoIsSendList);
		ListboxUtils.rendererForEdit(bean.getIsmoney(),
				nodeVoIsSendList);
		
		List<NodeVo> List = new ArrayList<NodeVo>();
		String[][] is = {{"是", "是"}, {"否", "否"}};
		for (int i = 0; i < is.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(is[i][j]);
				nodeVo.setName(is[i][j + 1]);
			}
			List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPostponeFlag(),
				List);
	}

	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffManageData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}
		uecOutStaffCostH = (UecOutStaffCostH) arg.get("uecOutStaffCostH");
		uecOutStaffManage = UecOutStaffManage.newInstance();
		uecOutStaffManage = (UecOutStaffManage) arg.get("uecOutStaffManage");
		UecOutStaffManage uecOutStaffManageObj = UecOutStaffManage.newInstance();
		if (uecOutStaffManage != null) {
			PubUtil.fillPoFromEditorBean(uecOutStaffManage, bean);
		} else {
			uecOutStaffManage = uecOutStaffManageObj;
			PubUtil.fillPoFromEditorBean(uecOutStaffManage, bean);
		}
		if (opType.equals("add")) {
			uecOutStaffManage.setId(null);
			DateFormatter();
			uecOutStaffManage.setMonthId(null);
			uecOutStaffManage.addOnly();
			
			uecOutStaffCostH.setPkOutStaffManage(uecOutStaffManage.getId());
			uecOutStaffCostH.updateOnly();
			Events.postEvent("onOK", this.self, uecOutStaffManage);
			onCancel();
			Messagebox.show("新增成功！");
		} else if (opType.equals("edit")) {
			DateFormatter();
			uecOutStaffManage.setMonthId(null);
			uecOutStaffManage.updateOnly();
			Events.postEvent("onOK", this.self, uecOutStaffManage);
			onCancel();
			Messagebox.show("修改成功！");
		}
		
		onCancel();
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecOutStaffManageEditWin().onClose();
	}

	private String checkUacStaffManageData() {
		// BaseInfo Check
		/*if (StrUtil.isNullOrEmpty(bean.getJobName().getValue())) {
			return "请填写岗位名称";
		} else if (StrUtil.checkBlank(bean.getJobName().getValue())) {
			return "岗位名称中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getBeginDate().getValue())) {
			return "请填写在本单位从事外包开始日期";
		} else if (StrUtil.isNullOrEmpty(bean.getIsSCEO().getValue())) {
			return "请填写是否最小CEO";
		} else if (StrUtil.checkBlank(bean.getJobName().getValue())) {
			return "是否最小CEO中有空格";
		}*/

		// IDCARD Real Name Check
		/*if (EnumUacStaffInfo.CERT_TYPE_IDCARD.getValue().equals(
				bean.getCertType().getSelectedItem().getValue())) {
			String idNum = bean.getCertNumber().getValue();
			String idName = bean.getCertName().getValue();
			try {
				boolean isRealName = CertUtil.checkIdCard(idNum, idName);

				if (!isRealName) {
					return "实名认证未通过";
				}
			} catch (Exception e) {
				return "调用国政通接口失败";
			}

		}*/

		return null;
	}
	/**
	 * 字符串转date
	 */
	private void strToDate() {
		if (arg.get("uecOutStaffManage") != null) {
			uecOutStaffManage = (UecOutStaffManage) arg.get("uecOutStaffManage");
			PubUtil.fillBeanFromPo(uecOutStaffManage, bean);
			if (!StrUtil.isEmpty(uecOutStaffManage.getCtBeginDate())) {
				bean.getCtBeginDate().setValue(DateUtil.patternStrToDate(uecOutStaffManage.getCtBeginDate()));
			}
			if (!StrUtil.isEmpty(uecOutStaffManage.getCtEndDate())) {
				bean.getCtEndDate().setValue(DateUtil.patternStrToDate(uecOutStaffManage.getCtEndDate()));
			}
		}
	}
	public void DateFormatter() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getCtBeginDate().getValue())) {
			uecOutStaffManage.setCtBeginDate(formatter.format(bean.getCtBeginDate()
					.getValue()));
			uecOutStaffManage.setCtEndDate(formatter.format(bean.getCtEndDate()
					.getValue()));
		}
	}
}
